package com.jimmyhsu.ecnudaowei.Utils;

import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by jimmyhsu on 2016/10/26.
 */

public class TextViewUtils {
    public static void bindTextToTextView(String s, TextView tv) {
        if (s != null && tv != null) {
            tv.setText(s);
        }
    }

    public static String dateToTimeBefore(Date date){
        Date now=new Date();
        long timeBefore=(now.getTime()-date.getTime())/1000;
        if(timeBefore<60){
            return timeBefore+"秒前";
        }
        timeBefore/=60;
        if(timeBefore<60){
            return timeBefore+"分钟前";
        }
        timeBefore/=60;
        if(timeBefore<24){
            return timeBefore+"小时前";
        }
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        Calendar nowCalendar=Calendar.getInstance();
        if(calendar.get(Calendar.YEAR)==nowCalendar.get(Calendar.YEAR)){
            return (calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";
        }else{
            return calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";
        }
    }
}
